package cn.JHai.rbac.service.imp;

import cn.JHai.rbac.domain.SystemDictionary;
import cn.JHai.rbac.mapper.SystemDictionaryMapper;
import cn.JHai.rbac.query.PageResult;
import cn.JHai.rbac.query.QueryObject;
import cn.JHai.rbac.service.SystemDictionaryService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SystemDictionaryImp implements SystemDictionaryService {

    @Autowired
    private SystemDictionaryMapper systemDictionaryMapper;

    @Override
    public int Delete(Long id) {
        return systemDictionaryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public SystemDictionary get(Long id) {
        SystemDictionary sd =  systemDictionaryMapper.selectByPrimaryKey(id);

        return sd;
    }

    @Override
    public List<SystemDictionary> findAll() {
        return systemDictionaryMapper.selectAll();
    }

    @Override
    public int saveOrUpdata(SystemDictionary systemDictionary) {
        //是插入
        if(systemDictionary.getId()==null){
            return systemDictionaryMapper.insert(systemDictionary);
        }else {
            //跟新
            return systemDictionaryMapper.updateByPrimaryKey(systemDictionary);
        }
    }

    @Override
    public PageResult query(QueryObject queryObject) {

        Long count = systemDictionaryMapper.selectForCount(queryObject);
        List<SystemDictionary> list =systemDictionaryMapper.pageSelect(queryObject);
        for (SystemDictionary sd : list) {
            JSONObject so = new JSONObject();
            so.put("id",sd.getId());
            so.put("sn",sd.getSn());
            so.put("title",sd.getTitle());
            so.put("intro",sd.getIntro());
            sd.setJson(so);
        }
        /*if(count<=3l){
            //systemDictionarys = systemDictionaryMapper.selectAll();
        }
        else{
            //systemDictionarys = systemDictionaryMapper.pageSelect(queryObject);
        }*/
        return new PageResult(queryObject.getCurrentPage(),queryObject.getPageSize(),list,count);
    }
}
